package com.ch.home.service.serviceImpl.mallServiceImpl;

import com.ch.home.bean.Commodity;
import com.ch.home.bean.CommodityRecord;
import com.ch.home.mapper.mallMapper.MallMapper;
import com.ch.home.service.serviceInterface.mallService.MallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;

/**
 * Created by CHEN on 2019/8/25.
 */
@Service
public class MallServiceImpl implements MallService {

    @Autowired
    MallMapper mallMapper;

    @Override
    public HashMap addCommodity(Commodity commodity) {
        HashMap<Object, Object> result = new HashMap<>( );
        int re = mallMapper.addCommodity(commodity);
        if (re == 1) {
            result.put("message", "seccess");
        } else {
            result.put("message", "fail");
        }
        return result;
    }

    @Override
    public HashMap updateCommodity(Commodity commodity) {

        HashMap<Object, Object> result = new HashMap<>( );
        int re = mallMapper.updateCommodity(commodity);
        if (re == 1) {
            result.put("message", "seccess");
        } else {
            result.put("message", "fail");
        }
        return result;
    }

    @Override
    public HashMap getCommodityPreview(Commodity commodity) {
        HashMap<Object, Object> result = new HashMap<>( );

        LinkedList<Commodity> commodities = mallMapper.getCommodityInfo(commodity);
        if (commodities.size( ) > 0) {
            result.put("message", "success");
            for (Commodity c : commodities
                    ) {
                if (c.getCommodityDescription( ).length( ) > 10)
                    c.setCommodityDescription(c.getCommodityDescription( ).substring(0, 10) + "...");
            }
        } else {
            result.put("message", "fail");
        }
        result.put("value", commodities);
        return result;
    }

    @Override
    public HashMap getCommodityAll(Commodity commodity) {
        HashMap<Object, Object> result = new HashMap<>( );

        LinkedList<Commodity> commodities = mallMapper.getCommodityInfo(commodity);
        if (commodities.size( ) > 0) {
            result.put("message", "success");
        } else {
            result.put("message", "fail");

        }
        result.put("value", commodities);
        return result;
    }

    @Override
    public HashMap generateOrder(Integer userId, Integer commodityId, Integer num) {
        HashMap<Object, Object> result = new HashMap<>( );
        Commodity commodity = new Commodity( );
        commodity.setCommodityId(commodityId);
        //1.拿到商品信息，拿到商品信息
        LinkedList<Commodity> commodityInfo = mallMapper.getCommodityInfo(commodity);
        if (commodityInfo.size( ) > 0) {
            result.put("message", "success");
        } else {
            result.put("message", "fail");
            result.put("value", null);
            return result;
        }
        //2.计算出金额，生成时间
        Integer sum = commodityInfo.get(0).getCommodityPrice( ) * commodityInfo.get(0).getCommodityDiscount( ) / 10 * num;
        //封装成map
        HashMap<Object, Object> value = new HashMap<>( );
        value.put("userId", userId);
        value.put("commodityName", commodityInfo.get(0).getCommodityName( ));
        value.put("commodityPrice", commodityInfo.get(0).getCommodityPrice( ));
        value.put("time", new Date( ));
        value.put("commodityUrl", commodityInfo.get(0).getCommodityUrl( ));
        value.put("num", num);
        value.put("sum", sum);
        result.put("value", value);
        return result;
    }

    @Override
    public HashMap buy(CommodityRecord commodityRecord) {

        HashMap<Object, Object> result = new HashMap<>( );

        int re = mallMapper.insertInfo(commodityRecord);

        if (re == 1) {
            result.put("message", "seccess");
        } else {
            result.put("message", "fail");
        }
        return result;

    }

    @Override
    public HashMap getRecord(CommodityRecord commodityRecord) {
        HashMap<Object, Object> result = new HashMap<>( );

        LinkedList<CommodityRecord> recordInfo = mallMapper.getRecordInfo(commodityRecord);

        if (recordInfo.size( ) > 0) {
            result.put("message", "seccess");
        } else {
            result.put("message", "fail");
        }
        result.put("value", recordInfo);
        return result;
    }
}
